﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraReports.UI;
using System.IO;
namespace Exact_VAS
{
    public partial class MainForm : DevExpress.XtraEditors.XtraForm
    {
        ExactDataContext DS;
      
        public MainForm(string flag)
        {
            InitializeComponent();
            DS = new ExactDataContext();
            DS.Connection.ConnectionString = this.CheckConnStrFile();
            this.LoadForm(flag);
        }
        private void LoadForm(string flag)
        {
            if (flag == "0")
            {
                XtraForm frm = this.CheckExistChildForm(typeof(BankPaymentVoucherForm));
                if (frm != null)
                    frm.Activate();
                else
                {
                    BankPaymentVoucherForm frm1 = new BankPaymentVoucherForm(DS);
                    frm1.MdiParent = this;
                    frm1.Show();
                }
            }
            if (flag == "1")
            {
                XtraForm frm = this.CheckExistChildForm(typeof(PaymentVoucherForm));
                if (frm != null)
                    frm.Activate();
                else
                {
                    PaymentVoucherForm frm1 = new PaymentVoucherForm(DS);
                    frm1.MdiParent = this;
                    frm1.Show();
                }
            }
            if (flag == "2")
            {
                XtraForm frm = this.CheckExistChildForm(typeof(JournalVoucherForm));
                if (frm != null)
                    frm.Activate();
                else
                {
                    JournalVoucherForm frm1 = new JournalVoucherForm(DS);
                    frm1.MdiParent = this;
                    frm1.Show();
                }
            }

            if (flag == "3")
            {
                XtraForm frm = this.CheckExistChildForm(typeof(DebtorStatementForm));
                if (frm != null)
                    frm.Activate();
                else
                {
                    DebtorStatementForm frm1 = new DebtorStatementForm(DS);
                    frm1.MdiParent = this;
                    frm1.Show();
                }
            }
  
        }
        private string CheckConnStrFile()
        {
            FileInfo fileConn = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\ConnExact.txt");
            string connStr = "";
            if (!fileConn.Exists)
            {
                StreamWriter writer = fileConn.CreateText();
                writer.WriteLine("CONNSTR|Data Source=isit-exac;Initial Catalog=300;Integrated Security=True");

                writer.Flush();
                writer.Close();


                StreamReader reader = fileConn.OpenText();
                while (reader.Peek() > 0)
                {
                    string text = reader.ReadLine();
                    connStr = text.Split(new char[1] { '|' })[1];
                }

            }
            else
            {

                StreamReader reader = fileConn.OpenText();
                while (reader.Peek() > 0)
                {
                    string text = reader.ReadLine();
                    connStr = text.Split(new char[1] { '|' })[1];
                }
            }
            return connStr;

        }
        private XtraForm CheckExistChildForm(Type formType)
        {
            foreach (XtraForm f in this.MdiChildren)
            {
                if (f.GetType() == formType)
                    return f;
            }
            return null;
        }
       

        private void menuItem_BankPaymentVoucher_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(BankPaymentVoucherForm));
            if (frm != null)
                frm.Activate();
            else
            {
                BankPaymentVoucherForm frm1 = new BankPaymentVoucherForm(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

        private void btnCashPayment_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(PaymentVoucherForm));
            if (frm != null)
                frm.Activate();
            else
            {
                PaymentVoucherForm frm1 = new PaymentVoucherForm(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }

        private void btnJournalVoucher_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraForm frm = this.CheckExistChildForm(typeof(JournalVoucherForm));
            if (frm != null)
                frm.Activate();
            else
            {
                JournalVoucherForm frm1 = new JournalVoucherForm(DS);
                frm1.MdiParent = this;
                frm1.Show();
            }
        }
    }
}